<html>
	<head>
		<title>
			To test datahelper.js
		</title>
		<script type="text/javascript" src="data.js"></script>
		<!-- uncomment below line for data helper testing -->
		<script type="text/javascript" src="datahelper.js"></script>
		<script type="text/javascript">
			var helper;
			var menuObject;
			var inhibitMessages = true;
			var scriptCallback = init;
			
			function init() {
			    menuObject = new vg.visualization.PaginatedList({
			        "container": "testGrid",
			        "listName": "Project Costs",
			        "headers": [{
						"displayName": "Line No.",
						"width": "100px",
						"dataName": "LINENUM",
						"isIdentifying": true
					}],
			        "numRows": 10,
					"dataCursorSize" : 25,
					"events" : {
						"onnextneeded" : onnextneeded,
						"onpreviousneeded" : onpreviousneeded,
						"onlastneeded" : onlastneeded,
						"onfirstneeded" : onfirstneeded
					},
			        "rowHeight": "20px"
			    });
			    menuObject.setFirstData(getData("first"));
			}

			function runHelperTestCases() {
				helper = new DataHelper({
					"idHeaders": [{
						displayName: "Line No.",
						width: "100px",
						dataName: "LINENUM",
						isIdentifying: true
					}],
					"dataCursorSize" : 25,
					"numRows" : 10,
					"events" : {
						"onnextneeded" : onnextneeded,
						"onpreviousneeded" : onpreviousneeded,
						"onlastneeded" : onlastneeded,
						"onfirstneeded" : onfirstneeded
					}
				});
				testCase1();
				isCasePassed("Test Case 1");
				testCase2();
				isCasePassed("Test Case 2");
				testCase3();
				isCasePassed("Test Case 3");
				testCase4();
				isCasePassed("Test Case 4");
			}

			/**
			 * Navigate from first to last and then again from last to first
			 * Only onfirstneeded and onnextneeded are to be fired
			 * @return {[type]} [description]
			 */
			function testCase1() {
				var data = helper.getFirstData();
				print("first data",data);
				data = helper.getNextData();
				print("next data 1",data);
				data = helper.getNextData();
				print("next data 2",data);
				data = helper.getNextData();
				print("next data 3",data);
				data = helper.getNextData();
				print("next data 4",data);
				data = helper.getPreviousData();
				print("previous data between nexts",data);				
				data = helper.getNextData();
				print("next data 5",data);
				data = helper.getNextData();
				print("next data 6",data);
				data = helper.getNextData();
				print("next data 7",data);
				data = helper.getLastData();
				print("last data",data);
				data = helper.getPreviousData();
				print("previous data 1",data);
				data = helper.getPreviousData();
				print("previous data 2",data);
				data = helper.getPreviousData();
				print("previous data 3",data);
				data = helper.getPreviousData();
				print("previous data 4",data);
				data = helper.getPreviousData();
				print("previous data 5",data);
				data = helper.getPreviousData();
				print("previous data 6",data);
				data = helper.getNextData();
				print("next data after reaching first by clicking previous",data);				
			}

			/**
			 * Navigate from last to first and then again from first to last
			 * Only onlastneeded and onpreviousneeded are to be fired
			 * @return {[type]} [description]
			 */
			function testCase2() {
				data = helper.getLastData();
				print("last data",data);
				data = helper.getPreviousData();
				print("previous data 1",data);
				data = helper.getPreviousData();
				print("previous data 2",data);
				data = helper.getPreviousData();
				print("previous data 3",data);
				data = helper.getPreviousData();
				print("previous data 4",data);
				data = helper.getPreviousData();
				print("previous data 5",data);
				data = helper.getPreviousData();
				print("previous data 6",data);
				var data = helper.getFirstData();
				print("first data",data);
				data = helper.getNextData();
				print("next data 1",data);
				data = helper.getNextData();
				print("next data 2",data);
				data = helper.getNextData();
				print("next data 3",data);
				data = helper.getNextData();
				print("next data 4",data);
				data = helper.getNextData();
				print("next data 5",data);
				data = helper.getNextData();
				print("next data 6",data);
				data = helper.getNextData();
				print("next data 7",data);				
			}

			/**
			 * Navigate from first to last and then back to first. With the next data set, the set is complete and
			 * the data should be frozen
			 * onfirstneeded, onlastneeded and onnextneeded should be fired once each in the order mentioned
			 * @return {[type]} [description]
			 */
			function testCase3() {
				var data = helper.getFirstData();
				print("first data",data);
				data = helper.getLastData();
				print("last data",data);
				var data = helper.getFirstData();
				print("first data again",data);				
				data = helper.getNextData();
				print("next data 1",data);
				data = helper.getNextData();
				print("next data 2",data);				
				data = helper.getLastData();
				print("last data again",data);
				data = helper.getPreviousData();
				print("previous data 1",data);
			}

			/**
			 * Navigate from first to last. With the fetching of the previous data set from last position, the set is 
			 * complete and the data should be frozen
			 * onfirstneeded, onlastneeded and onpreviousneeded should be fired once each in the order mentioned
			 * @return {[type]} [description]
			 */
			function testCase4() {
				var data = helper.getFirstData();
				print("first data",data);
				data = helper.getLastData();
				print("last data",data);
				data = helper.getPreviousData();
				print("previous data 1",data);
				data = helper.getPreviousData();
				print("previous data 2",data);
				var data = helper.getFirstData();
				print("first data again",data);				
				data = helper.getNextData();
				print("next data 1",data);
			}

			function isCasePassed(caseName) {
				if(arrayPassed(helper.firstData) && arrayPassed(helper.lastData)) {
					console.log(caseName + " Passed");
				}
			}

			function arrayPassed(dataArray) {
				return (dataArray.length == 6 && dataArray[0].length == 10 && dataArray[1].length == 10 && dataArray[2].length == 10 && dataArray[3].length == 10 && dataArray[4].length == 10 && dataArray[5].length == 8);
			}

			function onnextneeded(position, data) {
				!inhibitMessages && console.log("inside onnextneeded");
				var index = (position/25) - 1;
				return getData("next", index);
			}

			function onpreviousneeded(position, data) {
				!inhibitMessages && console.log("inside onpreviousneeded");
				var index = (position/25) - 1;
				return getData("previous", index);		
			}

			function onlastneeded(position, data) {
				!inhibitMessages && console.log("inside onlastneeded");
				return getData("last");
			}

			function onfirstneeded(position, data) {
				!inhibitMessages && console.log("inside onfirstneeded");
				return;
			}

			function print(msg, array) {
				if(inhibitMessages) {
					return;
				}
				var str = msg + " - ";
				if(array.length > 0) {
					str += array[0]["LINENUM"] + " || ";
					var last = array.slice(-1);
					str += last[0]["LINENUM"] + " || " + array.length + " || " + helper.currentPageNumber;
				}
				console.log(str);
			}
		</script>
	</head>
	<body>
		<div id="testGrid"></div>
		<script type="text/javascript" src="../paginated_list.js"></script>		
	</body>
</html>